﻿using UnityEngine;
using System.Collections;

public class SanghoGUIAnim : MonoBehaviour {

    public Texture2D tex;
    private Texture2D ntex;
    //private Texture2D newTex;

    private Vector2 texPos;
    public Vector2 gotoPos;

    public int u = 0;
    public int v = 0;

    public Matrix4x4 ddd;

    public int w =64;
    public int h =64;
    private int wc =0 ;
    private int hc =0 ;

	// Use this for initialization
	void Start () {
        print("width = " + Screen.width + "......."+ "height" + Screen.height);
        texPos = new Vector2(Screen.width/2, Screen.height/2);
        SplitImage();
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void SplitImage()
    {
        Color[] c = tex.GetPixels(u * 64, v * 64, w, h, 0);
		ntex = new Texture2D(w,h);	
		ntex.SetPixels(0,0,w,h,c);	
		ntex.Apply();	
    }

    IEnumerator GUIAnim()
    {
        float timeCount = 0;
        while (timeCount < 1f)
        {
            timeCount += Time.deltaTime;
            texPos = new Vector2(Mathf.SmoothStep(Screen.width / 2, Screen.width - 64, timeCount), Mathf.SmoothStep(Screen.height / 2, 0, timeCount));
            yield return null;
        }
    }

    void OnGUI()
    {
        if (GUILayout.Button("Action")) StartCoroutine("GUIAnim");
        if (GUILayout.Button("Reset")) texPos = new Vector2(Screen.width / 2, Screen.height / 2);
        if (ntex)
        {
            GUI.DrawTexture(new Rect(texPos.x, texPos.y, 64, 64), ntex, ScaleMode.ScaleAndCrop, true, 0);
        }
    }


}
